package com.ihr360.payroll.config;

import com.ihr360.commons.filter.EnumTypeFilter;
import com.ihr360.commons.lang.Enums;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.io.ResourceLoader;

/**
 * @author david.wei
 */
@Configuration
public class EnumConfig implements InitializingBean {
    @Autowired
    private Environment environment;

    @Autowired
    private ResourceLoader resourceLoader;
    @Override
    public void afterPropertiesSet() throws Exception {
        ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider(false);
        provider.setEnvironment(environment);
        provider.setResourceLoader(resourceLoader);
        provider.addIncludeFilter(new EnumTypeFilter());
        scanPersistModule(provider, "com.ihr360.payroll.model.type");
        scanPersistModule(provider, "com.ihr360.commons.model.type");
        scanPersistModule(provider, "com.ihr360.payroll.type");
    }

    private void scanPersistModule(ClassPathScanningCandidateComponentProvider provider,
                                   String packageName) throws ClassNotFoundException {
        for (BeanDefinition definition : provider.findCandidateComponents(packageName)) {
            Enums.addEnumAlias(definition.getBeanClassName());
        }
    }
}